<?php
//get方式curl
function getCurl($url){//get https的内容
    //初始化
    $ch = curl_init();
    //访问链接
    curl_setopt($ch, CURLOPT_URL,$url);
    //得到的内容输出形式false:直接输入true:返回数据
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);//不输出内容
    //对ssl证书验证
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    //执行
    $result =  curl_exec($ch);
    //关闭
    curl_close ($ch);
    return $result;
}
//更新token讲token信息存到文件中
function gettoken($appid,$secret){
    $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret;
    $token=@json_decode(file_get_contents('token.txt'),true);
    $token_time=$token['expires_in'];
    if ($token_time > time()) {
      return $token['access_token'];
    }
    $data= getCurl($url);//通过自定义函数getCurl得到https的内容
    $token = json_decode($data, true);//转为数组
    $token['expires_in']= $token['expires_in']+time()-50;
    $res=file_put_contents('token.txt',json_encode($token));
    return $token['access_token'];
}
function createMenu($data,$url1){
    //初始化
    $ch = curl_init();
    //curl网址
    curl_setopt($ch, CURLOPT_URL,$url1);
 
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    //对ssl证书的验证
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
 
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
 
    curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
    //设定post数据
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    //返回结果信息方式 false:直接输出;true:返回数据
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    //执行
    $tmpInfo = curl_exec($ch);
    //判断执行结果,返回结果信息
    if (curl_errno($ch)) {
 
        return curl_error($ch);
 
    }
 
    curl_close($ch);
 
    return $tmpInfo;
 
}
// // 自定义菜单
// $url1 = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".gettoken('wx884248916dd13f5b','f3f4948509add2d1c1fa37ead37acf1b');
// $data = '{
//      "button":[
//      {
//           "type":"click",
//           "name":"傅老大",
//           "key":"home"
//       },
//       {
//            "type":"scancode_push",
//            "name":"陶小妹",
//            "key":"introduct"
//       },
//       {
//            "name":"他两资料",
//            "sub_button":[
//             {
//                "type":"view",
//                "name":"老大裹照",
//                "url":"http://www.baidu.com/"
//             },
//             {
//                "type":"view",
//                "name":"小妹裹照",
//                "url":"https://"
//             }]
//        }]
// }';
// echo createMenu($data,$url1);
// //查询自定义菜单
// $abc=getCurl('https://api.weixin.qq.com/cgi-bin/menu/get?access_token='.gettoken('wx884248916dd13f5b','f3f4948509add2d1c1fa37ead37acf1b'));
// var_dump($abc);
// 删除自定义菜单
// $abc=getCurl('https://api.weixin.qq.com/cgi-bin/menu/delete?access_token='.gettoken('wx884248916dd13f5b','f3f4948509add2d1c1fa37ead37acf1b'));
// var_dump($abc);
// gettoken('wx884248916dd13f5b','f3f4948509add2d1c1fa37ead37acf1b');
// $data=json_encode($_POST);
// file_put_content('wechat.txt',$data);
// $token=gettoken('wx884248916dd13f5b','f3f4948509add2d1c1fa37ead37acf1b');
// $abc=getCurl("https://api.weixin.qq.com/cgi-bin/user/get?access_token=$token&next_openid=");
// https://api.weixin.qq.com/cgi-bin/user/get?access_token=ACCESS_TOKEN&next_openid=NEXT_OPENID
// var_dump($abc);
// 
// 
// 
// 发送模板消息
// $token=gettoken('wx884248916dd13f5b','f3f4948509add2d1c1fa37ead37acf1b');
// $url="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=$token";
// $data='{
//            "touser":"oySjswV0LK7PlgyKebgAp73HPKoc",
//            "template_id":"qa8sxw_vCq5Vau72UJIoHD6CJ6u5ycLlG2IcrotMjd8",
//            "url":"https:www.baidu.com",  
//            "data":{
//                    "first": {
//                        "value":"恭喜你购买成功！",
//                        "color":"#173177"
//                    },
//                    "keyword1":{
//                        "value":"巧克力",
//                        "color":"#173177"
//                    },
//                    "keyword2": {
//                        "value":"39.8元",
//                        "color":"#173177"
//                    },
//                    "remark":{
//                        "value":"欢迎再次购买！德玛                西亚呀呀呀呀呀呀呀           呀呀呀呀呀呀呀呀嘿嘿嘿嘿嘿",
//                        "color":"#173177"
//                    }
//            }
//        }';
// $res=createMenu($data,$url);
// 
// 获取全部用户openid
// $token=gettoken('wx884248916dd13f5b','f3f4948509add2d1c1fa37ead37acf1b');
// $url="https://api.weixin.qq.com/cgi-bin/user/get?access_token=$token";
// $res=getCurl($url);
// $res1=json_decode($res,true);
// $openids=$res1['data']['openid'];
// foreach ($openids as $key => $value) {
// 	//获取用户基本信息
// 	$url="https://api.weixin.qq.com/cgi-bin/user/info?access_token=$token&openid=$value&lang=zh_CH";
// 	$res=getCurl($url);
// 	$res1=json_decode($res,true);
// 	var_dump($res1);
// }

